package com.example.im.immsgdemo;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.example.im.immsgdemo.plugin.SampleExtensionModul;

import java.util.List;

import io.rong.imkit.DefaultExtensionModule;
import io.rong.imkit.IExtensionModule;
import io.rong.imkit.RongExtensionManager;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

public class ImChatManager implements RongIMClient.OnReceiveMessageListener, RongIMClient.ConnectionStatusListener {
    private static final String TAG = "ImChatManager";
    private static ImChatManager instance;

    public static ImChatManager getInstance() {
        synchronized (ImChatManager.class) {
            if (instance == null) {
                instance = new ImChatManager();
            }
        }
        return instance;
    }

    public ImChatManager() {

    }

    /**
     * 开始聊天
     *
     * @param mContext     上下文
     * @param targetUserId 聊天对象uid
     * @param name         用户名称
     */
    public void startChat(Context mContext, String targetUserId, String name) {
        if (TextUtils.isEmpty(targetUserId)) {
            return;
        }
        RongIM.getInstance().startPrivateChat(mContext, targetUserId, name);
    }


    /**
     * 设置各种消息监听  回调
     */
    public void init() {
        //连接监听
        RongIM.setConnectionStatusListener(this);

        //消息提醒
        RongIM.setOnReceiveMessageListener(this);

        //插件
        registerExtensionModule();

        //消息阅读回执
        RongIM.getInstance().setReadReceiptConversationTypeList(getConversation());

        RongIM.getInstance().enableNewComingMessageIcon(true);

        RongIM.getInstance().enableUnreadMessageIcon(true);

        //接受到消息 回调
        RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
            @Override
            public UserInfo getUserInfo(String uid) {
                getByUserId(uid);
                return null;
            }
        }, true);

    }

    private void registerExtensionModule() {
        List<IExtensionModule> moduleList = RongExtensionManager.getInstance().getExtensionModules();
        IExtensionModule defaultModule = null;
        if (moduleList != null) {
            for (IExtensionModule module : moduleList) {
                if (module instanceof DefaultExtensionModule) {
                    defaultModule = module;
                    break;
                }
            }
            if (defaultModule != null) {
                RongExtensionManager.getInstance().unregisterExtensionModule(defaultModule);
                RongExtensionManager.getInstance().registerExtensionModule(new SampleExtensionModul());
            }
        }


    }


    public Conversation.ConversationType[] getConversation() {
        final Conversation.ConversationType[] conversationTypes = {
                Conversation.ConversationType.PRIVATE,
                Conversation.ConversationType.GROUP
        };
        return conversationTypes;
    }


    /**
     * 根据用户id 获取用户名和头像
     *
     * @param uid 用户id
     */
    public void getByUserId(String uid) {
        if (TextUtils.isEmpty(uid)) {
            return;

        }
        // TODO: 2018/12/18  网络请求用户信息

    }


    /**
     * 连接融云
     *
     * @param token 融云 token
     */
    public void imConnon(String token) {
        if (TextUtils.isEmpty(token)) {
            return;
        }
        RongIM.connect(token, new RongIMClient.ConnectCallback() {
            @Override
            public void onTokenIncorrect() {
                Log.i(TAG, "onTokenIncorrect: ");
            }

            @Override
            public void onSuccess(String s) {
                Log.i(TAG, "onSuccess: 连接成功");
                Toast.makeText(MainApp.getContext(), "连接成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                Log.i(TAG, "onError: " + errorCode);
            }
        });
    }

    @Override
    public void onChanged(ConnectionStatus connectionStatus) {
        switch (connectionStatus) {
            case CONNECTED://连接成功。
                Log.i(TAG, "onChanged: 连接成功");

                break;
            case DISCONNECTED://断开连接。
                Log.i(TAG, "onChanged: 断开连接");
                break;
            case CONNECTING://连接中。
                Log.i(TAG, "onChanged: 连接中");
                break;
            case NETWORK_UNAVAILABLE://网络不可用。
                Log.i(TAG, "onChanged: 网络不可用");
                break;
            case KICKED_OFFLINE_BY_OTHER_CLIENT://用户账户在其他设备登录，本机会被踢掉线
                Log.i(TAG, "onChanged: 用户账户在其他设备登录");
                break;
        }
    }

    @Override
    public boolean onReceived(Message message, int i) {

        return false;
    }
}
